<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class MusicController extends Controller
{
    //音乐展示
    public function musiclist(){
        $res = DB::table('music')->paginate(1);
        return view('admin/music/music',['res'=>$res]);
    }
    //音乐添加
    public function musicadd(){
        return view('admin/music/musicadd');
    }

    public function musicadddata(Request $request){
        $title = $request->title;
        $artist = $request->artist;
        $mp3 =$request->mp3;
        $cover = $request->cover;
        $res = DB::table('music')->insert(['title'=>$title,'artist'=>$artist,'mp3'=>$mp3,'cover'=>$cover]);
        if($res != false){
            return redirect('admin/music');
        }
    }
    //修改音乐
    public function musicedit(){
        $id = $_GET['id'];
        $res = DB::table('music')->where('id',$id)->get();
        if ($res != null){
            return view('admin/music/musicedit',['res'=>$res]);
        }
    }

    public function musiceditdata(Request $request){
        $id = $request->id;
        $title = $request->title;
        $artist = $request->artist;
        $mp3 =$request->mp3;
        $cover = $request->cover;
        $res = DB::table('music')->where('id',$id)->update(['title'=>$title,'artist'=>$artist,'mp3'=>$mp3,'cover'=>$cover]);
        if ($res != false){
            return redirect('admin/music');
        }
    }

    public function musicdel(){
        $id = $_GET['id'];
        $res = DB::table('music')->where('id',$id)->delete();
        if ($res != false){
            return redirect('admin/music');
        }
    }
}
